package com.zhoulu.controller.borrowRecord;

import com.zhoulu.domain.borrowRecord.BorrowRecordSearch;
import com.zhoulu.domain.borrowRecord.BorrowRecordView;
import com.zhoulu.domain.user.UserBean;
import com.zhoulu.domain.util.LayuiResult;
import com.zhoulu.enums.RoleEnum;
import com.zhoulu.service.borrowRecord.BorrowRecordService;
import com.zhoulu.util.PageRequest;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class BorrowRecordController {

    @Autowired
    private BorrowRecordService borrowRecordService;

    /**
     * 查询借阅记录集合
     * @param search
     * @return
     */
    @RequestMapping("pageBorrowRecord")
    public LayuiResult pageBorrowRecord(BorrowRecordSearch search, PageRequest pageRequest){
        try{
            UserBean userBean = (UserBean) SecurityUtils.getSubject().getPrincipal();
            if(!RoleEnum.Admin.getCode().equals(userBean.getRoleCode())){
                search.setUsername(userBean.getUsername());
            }
            PageInfo<BorrowRecordView> pageInfo = borrowRecordService.pageBorrowRecord(search, pageRequest);
            return LayuiResult.success("查询成功",pageInfo.getTotal(), pageInfo.getList());
        }catch(Exception error){
            error.printStackTrace();
            return LayuiResult.error(error.getMessage());
        }
    }

    /**
     * 还书
     * @param id 借阅记录id
     * @return
     */
    @RequestMapping("returnBook")
    public LayuiResult returnBook(Integer id){
        try{
            return borrowRecordService.returnBook(id);
        }catch(Exception error){
            error.printStackTrace();
            return LayuiResult.error(error.getMessage());
        }
    }
}
